'''
Created on 25/08/2010

@author: kivson
'''
from ftplib import FTP
from negocio.criptografia import Cripter

class FTPCripto(FTP):
    '''
    adicionando minha criptografia ao envio da biblioteca de FTP padro do python
    '''
    cripto = Cripter()

    def __init__(self, host='', user='', passwd=''):
        '''
        Constructor
        '''
        FTP.__init__(self, host, user, passwd)
    
    
    def envia(self, arquivo, blocksize=8192, callback=None):
        arquivoDisco = file(arquivo.nomeCompleto())
        self.storbinaryCripto('STOR ' + arquivo.md5, arquivoDisco, blocksize, callback)
    
    def storbinaryCripto(self, cmd, fp, blocksize=8192, callback=None):
        """Store a file in binary mode.  A new port is created for you.
        
        >>>Adicionado suporte a minha criptografia<<< 
        
        Args:
          cmd: A STOR command.
          fp: A file-like object with a read(num_bytes) method.
          blocksize: The maximum data size to read from fp and send over
                     the connection at once.  [default: 8192]
          callback: An optional single parameter callable that is called on
                    on each block of data after it is sent.  [default: None]

        Returns:
          The response code.
        """
        self.voidcmd('TYPE I')
        conn = self.transfercmd(cmd)
        while 1:
            buf = fp.read(blocksize)
            if not buf: break
            '''Adicionado suporte a criptografia'''
            conn.sendall(self.cripto.criptografa(buf))
            if callback: callback(buf)
        conn.close()
        return self.voidresp()



    def retrbinaryCripto(self, cmd, callback, blocksize=8192, rest=None):
        """Retrieve data in binary mode.  A new port is created for you.

        Args:
          cmd: A RETR command.
          callback: A single parameter callable to be called on each
                    block of data read.
          blocksize: The maximum number of bytes to read from the
                     socket at one time.  [default: 8192]
          rest: Passed to transfercmd().  [default: None]

        Returns:
          The response code.
        """
        self.voidcmd('TYPE I')
        conn = self.transfercmd(cmd, rest)
        while 1:
            data = conn.recv(blocksize)
            if not data:
                break
            data = self.cripto.criptografa(data)
            callback(data)
        conn.close()
        return self.voidresp()



if __name__ == '__main__':
    def progresso(*args):
        print '#',
    print 'teste:'
    ftp = FTPCripto('computeiros.org')
    ftp.login('kivson@computeiros.org', '741953')
    arq = file('/home/kivson/Documentos/Minhas musicas/Detonaltas/Detonautas - Outro Lugar Mp3.mp3','rb')
    ftp.storbinaryCripto('STOR '+'teste.mp3', arq, callback=progresso)
    
    out = file('/home/kivson/musica.mp3','wb')
    ftp.retrbinaryCripto('RETR teste.mp3', out.write)
    
    arq.close()
    out.close()
    ftp.close()